﻿/* Copyright (c) 2010 Lexalytics Inc.

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Lexalytics4Excel
{
    /// <summary>
    /// Logging class based on a thread-safe Singleton pattern. Writes log and separate signature file to 
    /// ensure verifiability of log contents. Based on http://en.csharp-online.net/Singleton_design_pattern:_Thread-safe_Singleton
    /// </summary>
    public sealed class Logger
    {
        private string LogFile = String.Format("{0}/Lexalytics/Lexalytics4Excel/trace.log", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
        private StreamWriter _swLog = null;

        Logger()
        {
        }

        /// <summary>
        /// Static instance of logger class used by consumers
        /// </summary>
        public static Logger Instance
        {
            get
            {
                return LogCore.instance;
            }
        }

        /// <summary>
        /// Outputs exception to log file
        /// </summary>
        /// <param name="LogError">Exception to log</param>
        public void WriteToLog(Exception LogError)
        {
            if (_swLog == null)
                _swLog = new System.IO.StreamWriter(LogFile, true);
            System.DateTime dtLog = DateTime.Now;
            _swLog.WriteLine(String.Format("{0}\t{1}", dtLog.ToString("yyyy-MM-ddTHH:mm:ss"), LogError.ToString()));
            if (LogError.InnerException != null)
                _swLog.WriteLine(String.Format("{0}\t{1}", dtLog.ToString("yyyy-MM-ddTHH:mm:ss"), LogError.InnerException.ToString()));
            _swLog.Flush();
        }

        /// <summary>
        /// Outputs message string to log file
        /// </summary>
        /// <param name="LogMessage">String to log</param>
        public void WriteToLog(string LogMessage)
        {
            if (_swLog == null)
                _swLog = new System.IO.StreamWriter(LogFile, true);
            System.DateTime dtLog = DateTime.Now;
            _swLog.WriteLine(String.Format("{0}\t{1}", dtLog.ToString("yyyy-MM-ddTHH:mm:ss"), LogMessage.ToString()));
            _swLog.Flush();
        }

        /// <summary>
        /// Public method to clear the log file
        /// </summary>
        public void ClearLog()
        {
            if (File.Exists(LogFile))
                File.Delete(LogFile);
        }

        class LogCore
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static LogCore()
            {
            }

            internal static readonly Logger instance = new Logger();
        }
    }
}
